package com.paw.portal.controller;


import java.util.List;

import javax.validation.Valid;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.paw.common.domain.Result;
import com.paw.common.util.BeanUtilTools;
import com.paw.portal.entity.SysRole;
import com.paw.portal.request.dto.SysRoleRequestDto;
import com.paw.portal.response.dto.SysRoleResponseDto;
import com.paw.portal.service.IAntdRoleService;
import com.paw.portal.service.ISysRoleService;
import com.paw.portal.util.QueryWrapperUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author Rubble
 * @since 2021-08-21
 */
@RestController
public class SysRoleController {

  @Autowired
  private ISysRoleService sysRoleService;

  @Autowired
  private IAntdRoleService antdRoleService;

  @ApiOperation("角色权限列表")
  @PostMapping("/api/portal/sysRole/pageList")
  public Result<List<SysRoleResponseDto>> getRolePageList(@Valid @RequestBody SysRoleRequestDto requestDto){
    LambdaQueryWrapper<SysRole> queryWrapper = QueryWrapperUtil.buildLambdaQueryWrapper(requestDto, SysRole.class);
    Page<SysRole> page = sysRoleService.page(new Page<>(requestDto.getCurrent(), requestDto.getPageSize()), queryWrapper);
    List<SysRoleResponseDto> dtos = BeanUtilTools.copyList(page.getRecords(), SysRoleResponseDto.class);
    return Result.page(dtos,page.getTotal());
  }

  @ApiOperation("新增角色")
  @PostMapping("/api/portal/sysRole/addRole")
  public Result<SysRoleResponseDto> addRole(@Valid @RequestBody SysRoleRequestDto requestDto){
    SysRole role = new SysRole();
    BeanUtils.copyProperties(requestDto,role);
    sysRoleService.save(role);
    SysRoleResponseDto dto = new SysRoleResponseDto();
    BeanUtils.copyProperties(role,dto);
    return Result.success(dto);
  }

  @ApiOperation("修改角色")
  @PostMapping("/api/portal/sysRole/updateRole")
  public Result<SysRoleResponseDto> updateRole(@Valid @RequestBody SysRoleRequestDto requestDto){
    Assert.notNull(requestDto.getId(),"id不能为空");
    SysRole role = new SysRole();
    BeanUtils.copyProperties(requestDto,role);
    sysRoleService.updateById(role);
    SysRoleResponseDto dto = new SysRoleResponseDto();
    BeanUtils.copyProperties(role,dto);
    return Result.success(dto);
  }


  @ApiOperation("删除角色")
  @PostMapping("/api/portal/sysRole/delRole/{:id}")
  public Result<Boolean> delRole(@PathVariable(value = "id") Long id){
    return antdRoleService.delRoleWithMenu(id);
  }

}
